package com.auxgroup.hr.front.commons.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.apache.commons.lang.StringUtils;  

/**
 * com.util.AmountUtils 
 * @description  金额元分之间转换工具类 
 * @author qunxing.wang
 * 
 */ 
public class AmountUtils {  
      
    /**金额为分的格式 */  
    public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";  
      
      
    
    /**
     * 判断输入的字符串参数是否为空
     * @return boolean 空则返回true,非空则flase
     */
    public static boolean isEmpty(String input) {
        return null==input || 0==input.length() || 0==input.replaceAll("\\s", "").length();
    }
    
    /**  
     * 将元为单位的转换为分 （乘100） 
     *  
     * @param amount 
     * @return 
     */  
    public static String changeY2F(Long amount){  
        return BigDecimal.valueOf(amount).multiply(new BigDecimal(100)).toString();  
    }  
      
    /**  
     * 将元为单位的转换为分 替换小数点，支持以逗号区分的金额 
     *  
     * @param amount 
     * @return 
     */  
    public static String changeY2F(String amount){  
        String currency =  amount.replaceAll("\\$|\\￥|\\,", "");  //处理包含, ￥ 或者$的金额  
        int index = currency.indexOf(".");  
        int length = currency.length();  
        Long amLong = 0l;  
        if(index == -1){  
            amLong = Long.valueOf(currency+"00");  
        }else if(length - index >= 3){  
            amLong = Long.valueOf((currency.substring(0, index+3)).replace(".", ""));  
        }else if(length - index == 2){  
            amLong = Long.valueOf((currency.substring(0, index+2)).replace(".", "")+0);  
        }else{  
            amLong = Long.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");  
        }  
        return amLong.toString();  
    }  
      
	/**
	 * 将分格式的数值转换为元格式的数值 如1234->12.34,123->1.23
	 * 
	 * @param amount
	 * @return
	 */
	public static String transferCent2Yuan(String amount) {
		if (StringUtils.isBlank(amount) || !StringUtils.isNumeric(amount)) {
			return amount;
		}
		amount=BigDecimal.valueOf(Double.parseDouble(amount)).stripTrailingZeros().toPlainString();
		if (amount.length() == 1) {
			return "0.0" + amount;
		} else if (amount.length() == 2) {
			return "0." + amount;
		} else {
			return amount.substring(0, amount.length() - 2) + "." + amount.substring(amount.length() - 2);
		}
	}
	
	/**
	 * 金额格式化
	 * @param amt 金额
	 * @return 格式后的金额
	 */
	public static String insertComma(String amt) {
	    if (amt == null || amt.length() < 1) {
	        return "";
	    }
	    NumberFormat formater = new DecimalFormat("###,##0.00");
	    double num = Double.parseDouble(amt);
	    return formater.format(num);
	}
	/**
	 * 金额去掉“,”
	 * @param amt 金额
	 * @return 去掉“,”后的金额
	 */
	public static String delComma(String amt) {
	    String formatString = "";
	    if (amt != null && amt.length() >= 1) {
	        formatString = amt.replaceAll(",", "");
	    }
	    return formatString;
	}
}  
